﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using WcfMasterSlaveElectionService;

namespace MasterSlaveConsole
{
    public class LogClient : ILog
    {
        public LogClient()
        {
        }


        public void LogMessage(string fmt, params object[] arg)
        {
            string msg = string.Format(fmt, arg);
            Trace.WriteLine(DateTime.Now.ToString("HHmmss") + ": " + msg);
            Console.WriteLine(DateTime.Now.ToString("HHmmss") + ": " + msg);
        }


        public void LogMessage(Exception ex, string fmt, params object[] arg)
        {
            string msg = string.Format(fmt, arg);
            Trace.WriteLine(DateTime.Now.ToString("HHmmss") + ": " + msg);
            Console.WriteLine(DateTime.Now.ToString("HHmmss") + ": " + msg);

            LogException(ex);
        }

        private void LogException(Exception ex)
        {
            Trace.WriteLine("Message:" + ex.Message);
            Console.WriteLine("Message:" + ex.Message);
            Trace.WriteLine("Source:" + ex.Source);
            Console.WriteLine("Source:" + ex.Source);
            Trace.WriteLine("StackTrace:" + ex.StackTrace);
            Console.WriteLine("StackTrace:" + ex.StackTrace);

            if (ex.InnerException != null)
            {
                Trace.WriteLine("-== INNER EXCEPTION ==-");
                Console.WriteLine("-== INNER EXCEPTION ==-");
                LogException(ex.InnerException);
            }
        }
    }
}